#ifndef PLOTTER_H
#define PLOTTER_H

#include <QMap>
#include <QPixmap>
#include <QVector>
#include <QWidget>

class BusiCoord;



typedef struct Bar
{
	int index;
	double open;
	double close;
	double high;
	double low;
}Bar;


typedef struct ZoomSeetings
{
	double distance;;
	double barwidth;
}ZoomSeetings;




class QLabel;
class QLineEdit;
class QSpacerItem;
class QDockWidget;

class Plotter : public QWidget
{
    Q_OBJECT

public:
    Plotter(QVector<Bar>* pbars, QWidget *parent = 0);

	//QSize minimumSizeHint() const;
	//QSize sizeHint() const;



protected:
    void paintEvent(QPaintEvent *event);
    void resizeEvent(QResizeEvent *event);
	void keyPressEvent(QKeyEvent *event);
	void mousePressEvent(QMouseEvent *event);
	void mouseMoveEvent(QMouseEvent *event);
	void mouseReleaseEvent(QMouseEvent *event);

private:


	void RefreshBuffer();

	bool DrawGrid(QPainter *painter);

	bool DrawBars(QPainter *painter);


	void Scroll(int dx);

	typedef enum {zoomin = 1, zoomout} zoomset;
	void Zoom(zoomset option);

	void resizeORzoom();

private:

	QWidget *m_parent;

	enum { Margin = 50 };

	QRect m_rect;

	bool m_bCursorShown;
	QPoint m_cursorPoint;


	QPixmap pixmap;

	//Bars
	QVector<Bar>*  m_pBars;


	//ZoomSetting
	ZoomSeetings m_zoomset[6];
	unsigned int m_zoomIndex;

	//class used to record business coordinate
	BusiCoord* m_pBusiCoord;

	
public:

	QDockWidget* CreateBardockWidget();


private:

	QLabel *indexLabel;
	QLineEdit *indexLineEdit;
	QLabel *timeLabel;
	QLineEdit *timeLineEdit;
	QLabel *openLabel;
	QLineEdit *openLineEdit;
	QLabel *highLabel;
	QLineEdit *highLineEdit;
	QLabel *lowLabel;
	QLineEdit *lowLineEdit;
	QLabel *closeLabel;
	QLineEdit *closeLineEdit;
	QSpacerItem* spacerItem;
	QWidget* barWidget;
	QDockWidget* bardockWidget;

};


//class used to record business coordinate(another kind of coordinate is geography cooridinte)
class BusiCoord
{

public:

	BusiCoord(QRect& rect, QVector<Bar>* pbars, ZoomSeetings& zoomset);

	void ReInit(QRect& rect, ZoomSeetings& zoomset, int paraMaxIndex);

	//bool bValid(){return m_bValid;}

	bool GetBusiCoord(int& minXIndex, int& maxXIndex, double& minY, double& maxY, int& m_numYGridArea);

	bool Scroll(int dx);

private:

	int GetContainBarNum(QRect& rect, ZoomSeetings& zoomset);

	bool SetBusiCoord(QRect& rect, ZoomSeetings& zoomset, int paraMaxIndex);

	//unsigned int adjustAxis(int min, int max);

	int adjustAxis(double min, double max);

private:
	bool m_bValid;


	////Bars
	QVector<Bar>*  m_pBars;




	//index of display Bars in m_pBars
	int m_minXIndex;
	int m_maxXIndex;

	int m_focusXIndex;

	//minY and maxY value from  m_pBars[m_minXIndex] to m_pBars[m_maxXIndex]
	double m_minY;
	double m_maxY;

	//number of grid Area in Y coordinate
	int m_numYGridArea;

private:
    
};

#endif
